import BaseController from '@/lib/base-controller';
import { GET, POST, RequestMapping } from '@/lib/add-router';
// import auth from '@/lib/auth'


@RequestMapping('/api/role')
export default class RoleController extends BaseController {
  @POST('/add')
  async add() {
    const { ctx, service } = this;
    try {

      const role = await service.role.addRole(ctx.request.body);
      ctx.body = {
        code: 0,
        data: role,
      };
      this.success({
        code: 0,
        msg: '添加成功',
        data: role,
      });
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });

    }
  }
  @POST('/update')
  async update() {
    const { ctx, service } = this;
    try {

      const role = await service.role.updateRole(ctx.request.body);
      this.success({
        code: 0,
        msg: '修改成功',
        data: role,
      });

    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  @POST('/delete')
  async delete() {
    const { ctx, service } = this;
    try {
      const role = await service.role.deleteRole(ctx.request.body);
      this.success({
        code: 0,
        msg: '删除成功',
        data: role,
      });
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  @GET('/list')
  async list() {
    const { service } = this;
    try {
      const roleList = await service.role.getRoleList();
      this.success({
        code: 0,
        msg: '查询成功',
        data: roleList,
      });
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  @GET('/listByUserId')
  async listByUserId() {
    const { ctx, service } = this;
    try {

      const roleList = await service.role.getRoleListByUserId(ctx.query.userId);
      this.success({
        code: 0,
        msg: '查询成功',
        data: roleList,
      });
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  @GET('/getById')
  async getById() {
    const { ctx, service } = this;
    try {

      const role = await service.role.getRoleById(ctx.query.id);
      ctx.body = {
        code: 0,
        data: role,
      };
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  @GET('/getByName')
  async getByName() {
    const { ctx, service } = this;
    try {
      const role = await service.role.getRoleByName(ctx.query.name);
      ctx.body = {
        code: 0,
        data: role,
      };
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
  @GET('/getByCode')
  async getByCode() {
    const { ctx, service } = this;
    try {
      const role = await service.role.getRoleByCode(ctx.query.code);
      ctx.body = {
        code: 0,
        data: role,
      };
    } catch (err) {
      this.fail({
        code: 1,
        message: err,
      });
    }
  }
}
